﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Script.Serialization;
using InfoSource.Utilities;
using InfoSource.Models;
using InfoSource.Models.Views;
using InfoSource.Data;
using InfoSource.Models.objects;

namespace InfoSource.Controllers
{
	[HandleError]
	public class ToolsController : BaseController
	{
		
		public ActionResult Index()
		{
			return DynamicView();
		}

		/*----------------------------------------------------------*/
		public ActionResult ManageDoNotMarketList()
		{
			return DynamicView(new ManageDoNotMarketListViewModel());
		}

        /*----------------------------------------------------------*/
        public ActionResult GetPrintableDoNotMarketList(string outputType)
        {
            ManageDoNotMarketListViewModel model = new ManageDoNotMarketListViewModel();
            model.PrintOutputType = outputType;
            
            return DynamicView(model);
        }

		/*----------------------------------------------------------*/
		public ActionResult SaveGeoCodesToDB()
		{
			ViewBag["SerializedZipCodes"] = new JavaScriptSerializer().Serialize(new InfoSourceDB().colorado_zipcodes.Select(zip => zip.zip).ToList());
			return View();
			
		}

		/*----------------------------------------------------------*/
		public JsonResult SaveSingleGeoCodeToDB()
		{
			int Zip = (int.TryParse(Request["Zip"],out Zip) ? Zip : 0);
			decimal Lat = (decimal.TryParse(Request["Lat"], out Lat) ? Lat : 0);
			decimal Long = (decimal.TryParse(Request["Long"], out Long) ? Long : 0);
			int Index = (int.TryParse(Request["Index"], out Index) ? Index : 0);

			
			InfoSourceDB db = new InfoSourceDB();
			List<colorado_zipcode> COZipCodes = db.colorado_zipcodes.ToList();
			List<colorado_zipcode> COZipCodeMatches = COZipCodes.Where(zip => zip.zip == Zip).ToList();

			if (Zip != 0 && Lat != 0 && Long != 0 && COZipCodeMatches.Count > 0)
			{
				colorado_zipcode co_zip = COZipCodeMatches[0];

				co_zip.Latitude = Lat;
				co_zip.Longitude = Long;

				db.SaveChanges();
			}
			
			return Json(Index + ". " + Zip.ToString());
		}

		/*----------------------------------------------------------*/
		[HttpPost]
		public JsonResult DeleteSingleDoNotMarketZipCode()
		{
			int ZipCode = (int.TryParse(Request["zipcode"] ?? "0", out ZipCode) ? ZipCode : 0);

			ResultStatus results = DoNotMarketListRequests.DeleteSingleDoNotMarketZipCode(ZipCode);

			return Json(results);
		}

		/*----------------------------------------------------------*/
		[HttpPost]
		public JsonResult AddSingleDoNotMarketZipCode(string zip)
		{
			int ZipCode = (int.TryParse(zip, out ZipCode) ? ZipCode : 0);

			ResultStatus result = DoNotMarketListRequests.AddSingleDoNotMarketZipCode(ZipCode);

			return Json(result);
		}

		/*----------------------------------------------------------*/
		public JsonResult GetCoordinatesFromKMLFile(string zipcode)
		{
			List<List<LatLong>> latLong = Helpers.GetCoordinatesFromKMLFile(zipcode);														


			return Json(latLong, JsonRequestBehavior.AllowGet);

			//foreach (XElement placemark in placemarks)
			//{
			//    string name = placemark.Element("name").Value;

			//    if (!nameCoord.ContainsKey(name))
			//    {
			//        nameCoord[name] = new List<string>();
			//    }

			//    nameCoord[name].AddRange(placemark.Descendants("coordinates").Select(co => co.Value));
			//}
			

			
		}
		
	}
}
